package com.catsic.zcjc.convert;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.catsic.zcjc.constants.ZcjcConstants;
import com.catsic.zcjc.entity.City;
import com.catsic.zcjc.entity.Equip;
import com.catsic.zcjc.entity.EquipData;
import com.catsic.zcjc.entity.EquipFileData;
import com.catsic.zcjc.entity.EquipMaintain;
import com.catsic.zcjc.entity.MarkLabel;
import com.catsic.zcjc.entity.Province;
import com.catsic.zcjc.entity.Station;
import com.catsic.zcjc.entity.System;
import com.catsic.zcjc.enums.DataCountType;
import com.catsic.zcjc.enums.EquipDataTag;
import com.catsic.zcjc.enums.ZcjcStatus;
import com.catsic.zcjc.form.EquipDataForm;
import com.catsic.zcjc.form.EquipFileDataForm;
import com.catsic.zcjc.form.EquipForm;
import com.catsic.zcjc.form.EquipMaintainForm;
import com.catsic.zcjc.form.MarkLabelForm;
import com.catsic.zcjc.form.StationCountForm;
import com.catsic.zcjc.form.StationForm;
import com.catsic.zcjc.form.SystemForm;
import com.catsic.zcjc.tree.ZcjcTree;
import com.catsic.zcjc.util.StringUtil;

public class ZcjcConverts {

	public static StationForm convert(Station station){
		StationForm form = new StationForm();
		form.setId(station.getId());
		form.setName(station.getName());
		form.setAddress(station.getAddress());
		
		form.setCode(station.getCode());
		form.setLatitude(station.getLatitude());
		form.setLongitude(station.getLongitude());
		form.setRemark(station.getRemark());
		//form.setStatus(station.getStatus());
		//form.setCnStatus(ZcjcStatus.getName(station.getStatus()));
		form.setType(station.getType());
		form.setTypeSon(station.getTypeSon());
		//form.setCnType(StationType.getName(station.getType()));
		
		if(station.getOrgan() != null){
			form.setOrganId(station.getOrgan().getId());
			form.setOrganName(station.getOrgan().getName());
		}
		
		if(station.getCityCode()!=null){
			form.setDistrictId(station.getCityCode().getId());
			form.setDistrictName(station.getCityCode().getName());
			
			City city = station.getCityCode().getCity();
			Province p = city.getProvince();
			
			form.setCityId(city.getId());
			form.setCityName(city.getName());
			form.setProvinceId(p.getId());
			form.setProvinceName(p.getName());
		}
		
		return form;
	}
	
	public static SystemForm convert(System system){
		SystemForm form = new SystemForm();
		form.setId(system.getId());
		form.setName(system.getName());

		form.setCode(system.getCode());

		form.setRemark(system.getRemark());
		form.setStatus(system.getStatus());
		form.setCnStatus(ZcjcStatus.getName(system.getStatus()));
		
		form.setType(system.getType());
		//form.setCnType(StationType.getName(system.getType()));
		
		if(system.getStation() != null){
			form.setStationId(system.getStation().getId());
			form.setStationName(system.getStation().getName());
		}
		
		
		return form;
	}
	
	public static EquipForm convert(Equip equip){
		EquipForm form = new EquipForm();
		form.setId(equip.getId());
		form.setName(equip.getName());
		form.setCode(equip.getCode());
		form.setRemark(equip.getRemark());
		form.setMfr(equip.getMfr());
		form.setModel(equip.getModel());
		form.setInstallTime(equip.getInstallTime());
		form.setStatus(equip.getStatus());
		form.setCnStatus(ZcjcStatus.getName(equip.getStatus()));
		form.setType(equip.getType());
		//form.setCnType(StationType.getName(equip.getType()));
		
		if(equip.getStation() != null){
			form.setStationId(equip.getStation().getId());
			form.setStationName(equip.getStation().getName());
		}
		
		if(equip.getSystem() != null){
			form.setSystemId(equip.getSystem().getId());
			form.setSystemName(equip.getSystem().getName());
		}
		
		return form;
	}
	
	public static EquipDataForm convert(EquipData equipData){
		EquipDataForm form = new EquipDataForm();
		form.setId(equipData.getId());
		form.setAxleNumber(equipData.getAxleNumber());
		form.setCollectionTime(equipData.getCollectionTime());
		form.setEquipId(equipData.getEquip().getId());
		form.setEquipName(equipData.getEquip().getName());
		form.setGoodsType(equipData.getGoodsType());
		form.setOverrunWeight(equipData.getOverrunWeight());
		form.setPlateColor(equipData.getPlateColor());
		form.setPlateNo(equipData.getPlateNo());
		form.setTotalWeight(equipData.getTotalWeight());
		form.setTrainType(equipData.getTrainType());
		form.setWheelNumber(equipData.getWheelNumber());
		form.setWheelWeight(equipData.getWheelWeight());
		Integer tag = equipData.getTag();
		String tagName = EquipDataTag.NO_READ.getName();
		form.setTag(EquipDataTag.NO_READ.getIndex());
		if(tag != null){
			tagName = EquipDataTag.getName(tag);
			form.setTag(tag);
		}
		form.setTagName(tagName);
		
		form.setStatus(equipData.getStatus());
		form.setCnStatus(ZcjcStatus.getName(equipData.getStatus()));
		
		form.setWheelNumber1(equipData.getWheelNumber1());
		form.setWheelNumber2(equipData.getWheelNumber2());
		form.setWheelNumber3(equipData.getWheelNumber3());
		form.setWheelNumber4(equipData.getWheelNumber4());
		form.setWheelNumber5(equipData.getWheelNumber5());
		form.setWheelNumber6(equipData.getWheelNumber6());
		form.setWheelNumber7(equipData.getWheelNumber7());
		form.setWheelNumber8(equipData.getWheelNumber8());
		form.setWheelNumber9(equipData.getWheelNumber9());
		form.setWheelNumber10(equipData.getWheelNumber10());
		
		form.setAxleBase1(equipData.getAxleBase1());
		form.setAxleBase2(equipData.getAxleBase2());
		form.setAxleBase3(equipData.getAxleBase3());
		form.setAxleBase4(equipData.getAxleBase4());
		form.setAxleBase5(equipData.getAxleBase5());
		form.setAxleBase6(equipData.getAxleBase6());
		form.setAxleBase7(equipData.getAxleBase7());
		form.setAxleBase8(equipData.getAxleBase8());
		form.setAxleBase9(equipData.getAxleBase9());
		form.setAxleBase10(equipData.getAxleBase10());
		
		form.setAxleWeight1(equipData.getAxleWeight1());
		form.setAxleWeight2(equipData.getAxleWeight2());
		form.setAxleWeight3(equipData.getAxleWeight3());
		form.setAxleWeight4(equipData.getAxleWeight4());
		form.setAxleWeight5(equipData.getAxleWeight5());
		form.setAxleWeight6(equipData.getAxleWeight6());
		form.setAxleWeight7(equipData.getAxleWeight7());
		form.setAxleWeight8(equipData.getAxleWeight8());
		form.setAxleWeight9(equipData.getAxleWeight9());
		form.setAxleWeight10(equipData.getAxleWeight10());
		
		form.setSpeed(equipData.getSpeed());
		
		if(equipData.getStation() != null){
			form.setStationId(equipData.getStation().getId());
			form.setStationName(equipData.getStation().getName());
		}
		
		if(equipData.getSystem() != null){
			form.setSystemId(equipData.getSystem().getId());
			form.setSystemName(equipData.getSystem().getName());
		}
		
		if(equipData.getEquip() != null){
			form.setEquipId(equipData.getEquip().getId());
			form.setEquipName(equipData.getEquip().getName());
		}
		
		return form;
	}
	
	public static EquipFileDataForm convert(EquipFileData fileData){
		EquipFileDataForm form = new EquipFileDataForm();
		form.setId(fileData.getId());
		form.setName(fileData.getFileName());

		
		form.setType(fileData.getType());
		//form.setCnType(StationType.getName(system.getType()));
		
		
		return form;
	}

	public static EquipMaintainForm convert(EquipMaintain maintain){
		EquipMaintainForm form = new EquipMaintainForm();
		form.setId(maintain.getId());
		form.setWorkmanCode(maintain.getWorkmanCode());
		form.setWorkmanName(maintain.getWorkmanName());
		form.setRecord(maintain.getRecord());
		form.setConclusion(maintain.getConclusion());
		form.setMaintainTime(maintain.getMaintainTime());
		//form.setType(maintain.getTheme());

		if(maintain.getEquip() != null){
			form.setEquipId(maintain.getEquip().getId());
			form.setEquipName(maintain.getEquip().getName());
			form.setEquipCode(maintain.getEquip().getCode());
		}

		return form;
	}
	public static StationCountForm convert(Map<String,Object> map){
		StationCountForm form = new StationCountForm();
		if(StringUtils.isNotBlank(map.get("id").toString())){
			form.setId(map.get("id").toString());
		}
		form.setAreaName(StringUtil.getMapStr(map, "areaName"));
		form.setTotalCount(StringUtil.getMapInt(map, "totalCount"));
		form.setLatitude(StringUtil.getMapDouble(map, "latitude"));
		form.setLongitude(StringUtil.getMapDouble(map, "longitude"));
		form.setTypeCount1(StringUtil.getMapInt(map, "total1"));
		form.setTypeCount2(StringUtil.getMapInt(map, "total2"));
		form.setTypeCount3(StringUtil.getMapInt(map, "total3"));
		form.setTypeCount4(StringUtil.getMapInt(map, "total4"));
		form.setTypeCount5(StringUtil.getMapInt(map, "total5"));
		
		return form;
	}
	
	public static StationCountForm convert(String type,Map<String,Object> map){
		StationCountForm form = new StationCountForm();
		form.setId(map.get("id").toString());
		form.setAreaName(StringUtil.getMapStr(map, "areaName"));
		
		if(DataCountType.train.toString().equals(type)){
			form.setTotalValue(StringUtil.getMapDouble(map, "totalCount",2));
		}else if(DataCountType.weight.toString().equals(type)){
			form.setTotalValue(StringUtil.getMapDouble(map, "totalCount",2));
		}else if(DataCountType.overrun.toString().equals(type)){
			form.setTotalValue(StringUtil.getMapDouble(map, "totalCount",2));
		}
		
		form.setLatitude(StringUtil.getMapDouble(map, "latitude"));
		form.setLongitude(StringUtil.getMapDouble(map, "longitude"));
		
		
		return form;
	}
	
	public static ZcjcTree convertTree(Station station){  
		ZcjcTree treeInfo = new ZcjcTree();
		treeInfo.setId("station_"+station.getId());
		treeInfo.setCode(station.getCode());
		treeInfo.setName("<span style='color:blue;'>"+station.getName()+"</span>");
		treeInfo.setPid(station.getOrgan() == null ? null : station.getOrgan().getId());
		
		return treeInfo;
	}
	
	public static ZcjcTree convertTree(System system){  
		ZcjcTree treeInfo = new ZcjcTree();
		treeInfo.setId("system_"+system.getId());
		treeInfo.setCode(system.getCode());
		treeInfo.setName("<span style='color:green;'>"+system.getName()+"</span>");
		treeInfo.setPid(system.getStation() == null ? null : system.getStation().getId());
		
		return treeInfo;
	}
	public static MarkLabelForm convert(MarkLabel markLabel){
		MarkLabelForm form = new MarkLabelForm();
		form.setId(markLabel.getId());
		form.setLabel(markLabel.getLabel());
		int type = markLabel.getType();
		form.setType(type);
		String typeName="";
		if(type == ZcjcConstants.LABEL_TYPE_COMMON){
			typeName = "公用";
		}
		if(type == ZcjcConstants.LABEL_TYPE_PRIVATE){
			typeName = "私有";
		}
		form.setTypeName(typeName);

		return form;
	}
}
